package cn.new18.testactivitytask.dragview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import cn.new18.testactivitytask.R;
import cn.new18.testactivitytask.utils.DisplayUtils;

/**
 * Created by RaoWei on 2016/12/29 14:16.
 */

public class DragFloatView extends FrameLayout {

    public static int BOTTOM_LAYOUT_HEIGHT = 55;
    public static int TOP_LAYOUT_HEIGHT = 25;
    private int bottomEdge;
    private Context context;
    private float dX;
    private float dY;
    private int lastAction;
    private ImageView liveImg;
    private ImageView mClose;
    private View.OnClickListener mOnClickListener;
    private float rdXD;
    private float rdXM;
    private float rdYD;
    private float rdYM;
    private int screenH;
    private int screenW;
    private int statusBarHeight;
    private float tX;
    private float tY;
    private int topEdge;

    public DragFloatView(Context context) {
        this(context, null);
    }

    public DragFloatView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DragFloatView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        prepareLength();
        initFloatView();
        initEvent();
    }

    /**
     * 处理事件监听
     */
    private void initEvent() {

    }

    /**
     * 初始化悬浮窗
     */
    private void initFloatView() {
        LayoutInflater.from(context).inflate(R.layout.float_view, this);
        this.liveImg = (ImageView) findViewById(R.id.float_img);
        StrengthenView();
    }

    //对floatView做更多的操作，如动画的开启
    private void StrengthenView() {

    }

    private void prepareLength() {
        this.topEdge = DisplayUtils.dip2px(context, TOP_LAYOUT_HEIGHT);
        this.bottomEdge = DisplayUtils.dip2px(context, BOTTOM_LAYOUT_HEIGHT);
        this.screenH = DisplayUtils.getHeightPx(context);
        this.screenW = DisplayUtils.getWidthPx(context);
        this.statusBarHeight = DisplayUtils.getStatusHeight(context);
    }

    /**
     * 核心，处理view的拖动
     *
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.e("TAG", "dragFloatView.onTouchEvent");
        switch (event.getActionMasked()) {
            default:
                return false;
            case MotionEvent.ACTION_DOWN:
                this.dX = getX() - event.getRawX();
                this.dY = getY() - event.getRawY();
                this.rdXD = event.getRawX();
                this.rdYD = event.getRawY();
                this.lastAction = 0;
                break;
            case MotionEvent.ACTION_MOVE:
            this.tY = event.getRawY() + this.dY;
            this.tX = event.getRawX() + this.dX;
            this.rdXM = (event.getRawX() - this.rdXD);
            this.rdYM = (event.getRawY() - this.rdYD);
            if (Math.abs(this.rdXM) + Math.abs(this.rdYM) < 16.0F) {  //滑动距离太大？？
                this.lastAction = 2;
                Log.e("TAG", "onTouchEvent: " + this.rdXM);
                Log.e("TAG", "onTouchEvent: " + this.rdYM);
                Log.e("TAG", "onTouchEvent: " + (Math.abs(this.rdXM) + Math.abs(this.rdYM)));
                break ;
            }

                if ((this.tY > this.topEdge + this.statusBarHeight) && (this.tY + getHeight() < this.screenH - this.bottomEdge - this.statusBarHeight)) {   //view限制上下移动的边界
                    setY(this.tY);  //上下正常移动
                    //if ((this.tX <= 0.0F) || (this.tX + getWidth() >= this.screenW)) {  //限制左右移动的边界
                        //左右移动
                        if (this.tX < 0.0F) {
                            setX(0.0f);
                        } else if (this.tX + getWidth() < this.screenW) {
                            setX(this.tX);
                        } else {
                            setX(this.screenW - getWidth());
                        }
                    //}
                    //setX(this.tX);  //左右移动不超过边界，进行左右移动
                } else {
                    if (this.tY <= this.topEdge + this.statusBarHeight) {
                        setY(this.topEdge + this.statusBarHeight);
                    } else if (this.tY + getHeight() < this.screenH - this.bottomEdge - this.statusBarHeight) {
                        setY(this.tY);
                    } else {
                        setY(this.screenH - getHeight() - this.bottomEdge - this.statusBarHeight);
                    }

                    //if ((this.tX <= 0.0F) || (this.tX + getWidth() >= this.screenW)) {  //限制左右移动的边界
                        //左右移动
                        if (this.tX < 0.0F) {
                            setX(0.0f);
                        } else if (this.tX + getWidth() < this.screenW) {
                            setX(this.tX);
                        } else {
                            setX(this.screenW - getWidth());
                        }
                    //}
                }

                if ((this.lastAction == 0) && (this.mOnClickListener != null)) {
                    this.mOnClickListener.onClick(this);
                }
                this.rdXD = 0.0F;
                this.rdYD = 0.0F;
                return true;

        }

        return true;
    }
    public void setFloatViewOnClickListener(View.OnClickListener paramOnClickListener)
    {
        this.mOnClickListener = paramOnClickListener;
    }
}


